package com.sunlg.smarthome.ui.warning;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.sunlg.smarthome.R;
import com.sunlg.smarthome.base.BaseFragment;

import com.sunlg.smarthome.contract.SecurityWarningContract;
import com.sunlg.smarthome.contract.presenter.SecurityWarningPresenter;
import com.sunlg.smarthome.data.entity.SecurityWarning;
import com.sunlg.smarthome.data.entity.Warning;
import com.sunlg.smarthome.ui.wedget.RecyclerViewDivider;

import java.util.ArrayList;
import java.util.List;


public class SecurityWarningFragment extends BaseFragment
        implements SecurityWarningContract.View{

    private RecyclerView rvWarningList;
    private WarningAdapter warningAdapter;
    private List<Warning> warningList;
    private SecurityWarningContract.Presenter presenter;

    public SecurityWarningFragment() {
    }

    @Override
    protected int getLayoutResourceId() {
        return R.layout.fragment_security_warning;
    }

    @Override
    protected void init() {
        warningList = new ArrayList<>();
        warningAdapter = new WarningAdapter(getActivity(), warningList);
        presenter = new SecurityWarningPresenter();
        presenter.attachView(this);
    }

    @Override
    protected void initView(View view) {
        rvWarningList = (RecyclerView)view.findViewById(R.id.rv_warning_list);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        rvWarningList.setLayoutManager(layoutManager);
        rvWarningList.setAdapter(warningAdapter);
        rvWarningList.addItemDecoration(new RecyclerViewDivider(getActivity(), LinearLayoutManager.VERTICAL));
    }

    @Override
    public void onStart() {
        super.onStart();
        presenter.loadAllSecurityWarning();
    }

    @Override
    public void loadSecurityWarningSuccess(List<SecurityWarning> warnings) {
        warningList.clear();
        warningList.addAll(warnings);
        warningAdapter.notifyDataSetChanged();
    }

    @Override
    public void showError(String msg) {
        toast(msg);
    }
}
